package com.freeter.modules.controller;

import com.freeter.modules.gjs.service.FileWatcherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 文件监控服务控制器
 * 提供文件监控服务的REST API接口
 * 
 * @author freeter
 * @since 2024-01-01
 */
@RestController
@RequestMapping("/api/filewatcher")
public class FileWatcherController {
    
    @Autowired
    private FileWatcherService fileWatcherService;
    
    /**
     * 启动文件监控服务
     */
    @GetMapping("/start")
    public ResponseEntity<Map<String, Object>> startFileWatcher() {
        Map<String, Object> response = new HashMap<>();
        try {
            fileWatcherService.startFileWatcher();
            response.put("success", true);
            response.put("message", "文件监控服务启动成功");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "文件监控服务启动失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }
    
    /**
     * 停止文件监控服务
     */
    @PostMapping("/stop")
    public ResponseEntity<Map<String, Object>> stopFileWatcher() {
        Map<String, Object> response = new HashMap<>();
        try {
            fileWatcherService.stopFileWatcher();
            response.put("success", true);
            response.put("message", "文件监控服务停止成功");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "文件监控服务停止失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }
    
    /**
     * 获取文件监控服务状态
     */
    @GetMapping("/status")
    public ResponseEntity<Map<String, Object>> getFileWatcherStatus() {
        Map<String, Object> response = new HashMap<>();
        try {
            boolean isRunning = fileWatcherService.isRunning();
            response.put("success", true);
            response.put("running", isRunning);
            response.put("message", isRunning ? "文件监控服务正在运行" : "文件监控服务已停止");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "获取文件监控服务状态失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }
    
    /**
     * 重启文件监控服务
     */
    @PostMapping("/restart")
    public ResponseEntity<Map<String, Object>> restartFileWatcher() {
        Map<String, Object> response = new HashMap<>();
        try {
            fileWatcherService.stopFileWatcher();
            Thread.sleep(1000); // 等待1秒
            fileWatcherService.startFileWatcher();
            response.put("success", true);
            response.put("message", "文件监控服务重启成功");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "文件监控服务重启失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }
} 